package com.sc.web.gwt.client.view.uicomponent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;

/**
 * User: KienCT
 * Date: 01/07/2011
 * Time: 22:49
 */
public class ExpandablePanel extends Composite {
    private static Binder binder = GWT.create(Binder.class);
    Resources resources = GWT.create(Resources.class);

    public interface Binder extends UiBinder<HTMLPanel, ExpandablePanel> {
    }

    private boolean isExpanded = true;

    @UiField
    Image icon;

    @UiField
    Label title;

    @UiField
    HTMLPanel topPanel;

    @UiField
    HTMLPanel bottomPanel;

    @UiField
    HTMLPanel container;

    public ExpandablePanel() {
        initWidget(binder.createAndBindUi(this));
        resources.expandablePanelCSS().ensureInjected();
        icon.setResource(resources.collapse());
        icon.setStyleName(resources.expandablePanelCSS().icon());
    }

    @UiHandler("icon")
    public void iconCLickHandle(ClickEvent event) {
        if (isExpanded) {
            container.setStyleName(resources.expandablePanelCSS().collapsedContainer());
            bottomPanel.setHeight("2em");
            icon.setResource(resources.expand());
            isExpanded = false;
        } else {
            container.setStyleName(resources.expandablePanelCSS().expandedContainer());
            bottomPanel.setHeight("14em");
            icon.setResource(resources.collapse());
            isExpanded = true;
        }
    }

    public void setTitleLabel(String text) {
        title.setText(text);
    }

    public void addDataToTable(Widget widget) {
        bottomPanel.add(widget);
    }
}